package com.micangcaihang.micang.mvp.task.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.Properties;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.FragmentTaskWayStatusBinding;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.entity.TaskWayStatusEntity;
import com.micangcaihang.micang.http.request.TaskWayStatusRequest;
import com.micangcaihang.micang.mvp.task.contract.TaskWayStatusContract;
import com.micangcaihang.micang.mvp.task.presenter.TaskWayStatusPresenter;
import com.micangcaihang.micang.view.PickerViewHelper;

import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/24
 *     update : 2017/07/24
 *     desc   : 道路状况
 *     version: 1.0
 * </pre>
 */

public class TaskWayStatusFragment extends BaseFragment<TaskWayStatusPresenter, FragmentTaskWayStatusBinding> implements TaskWayStatusContract.View {
    private String id;
    private int pickerType;
    private TaskWayStatusRequest mRequest;
    private TaskWayStatusRequest mOldRequest;
    private boolean isNetSuccess;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }

    public static TaskWayStatusFragment newInstance(String id) {
        Bundle args = new Bundle();
        TaskWayStatusFragment fragment = new TaskWayStatusFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_way_status;
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    @Override
    protected void initData() {
        super.initData();
        mRequest = new TaskWayStatusRequest();
        mRequest.setId(id);
        mOldRequest = new TaskWayStatusRequest();
        mOldRequest.setId(id);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.setClickHandler(new ClickHandler());
    }

    /**
     * 返回下拉框信息
     *
     * @param list
     * @param type
     */
    @Override
    public void returnPickerViewData(List<PickerEntity> list, String type) {
        switch (pickerType) {
            case 1:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getRoad_level(), list,
                        pickerEntity1 -> {
                            mRequest.setRoad_level(pickerEntity1.getSign_id());
                            mViewBinding.tvRoadLevel.setText(pickerEntity1.getTitle());
                        });
                break;
            default:
                break;
        }
    }

    /**
     * 返回道路状况信息
     *
     * @param entity
     */
    @Override
    public void returnWayStatusData(TaskWayStatusEntity entity) {
        isNetSuccess = true;
        String[] excludsArray = new String[]{"choose_list", "id"};
        try {
            Properties.copyPropertiesExclude(entity, mRequest, excludsArray);
            Properties.copyPropertiesExclude(entity, mOldRequest, excludsArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //道路名称
        mViewBinding.etRoadName.setText(entity.getRoad_name());
        //直线距离
        mViewBinding.etRoadDistance.setText(entity.getRoad_distance());
        //地铁线路
        mViewBinding.etMetro.setText(entity.getMetro());
        //步行至地铁时间
        mViewBinding.etMetroDistance.setText(entity.getMetro_distance());
        //公交线路
        mViewBinding.etTransit.setText(entity.getTransit());
        //步行至公交时间
        mViewBinding.etTransitDistance.setText(entity.getTransit_distance());
        TaskWayStatusEntity.ChooseListBean chooseList = entity.getChoose_list();
        mViewBinding.tvRoadLevel.setText(chooseList.getRoad_level());
    }

    public class ClickHandler {
        public void onShowRoadLevelPickerView(View view) {
            pickerType = 1;
            mPresenter.loadPickerViewData("road_level", "道路等级");
        }

        public void onSave(View view) {
            saveData();
        }
    }

    /**
     * 开始保存数据请求
     */
    public void saveData() {
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            ToastUtils.showLong("您没有更改任何信息，无需保存");
            return;
        }
        isNetSuccess = false;
        mPresenter.saveWayStatusData(mRequest);
    }

    /**
     * 判断是否保存过数据 没有的话点击保存去保存
     *
     * @return 是否保存当前页面数据
     */
    public boolean isSaveData() {
        //如果请求错误则返回true
        if (!isNetSuccess) {
            return true;
        }
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * s设置请求数据
     */
    private void setSaveData() {
        mRequest.setRoad_name(mViewBinding.etRoadName.getText().toString().trim());
        mRequest.setRoad_distance(mViewBinding.etRoadDistance.getText().toString().trim());
        mRequest.setMetro(mViewBinding.etMetro.getText().toString().trim());
        mRequest.setMetro_distance(mViewBinding.etMetroDistance.getText().toString().trim());
        mRequest.setTransit(mViewBinding.etTransit.getText().toString().trim());
        mRequest.setTransit_distance(mViewBinding.etTransitDistance.getText().toString().trim());
    }
}
